""""
Case Type   : D库常用语法兼容-proc
Case Name   : D库支持创建存储过程。set schema 后执行正常
Create At   : 2025-11-19
Owner       : chen
Description :
    1.创建schame,设置当前schema
    2.创建proc
    3.创建新的schame,修改schame
    4.alter proc 修改schame,proc不带括弧
    5.alter procedure 修改schame
    6.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.合理报错
    5.成功
    6.成功
History     :
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node

log = Logger()
primary_sh = CommonSH('PrimaryDbUser')

class DDL_Proc_Case0002(unittest.TestCase):
    def setUp(self):
        log.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.dbname_d = Node('PrimaryDbDmode').db_name
        self.schema_name = 's_proc_schema_case002'
        self.new_schema_name = 's_proc_new_schema_case002'
        self.proc_name = 'p_proc_case002'

    def test_proc(self):
        text = '-----step1: 创建schame,设置当前schema; expect: 成功-----'
        log.info(text)
        msg = primary_sh.check_connection_status(self.constant.START_STATUS_MSG)
        self.assertTrue(msg)
        create_schema = f'create schema {self.schema_name};' \
            f'set current_schema to {self.schema_name};'
        res = primary_sh.execut_db_sql(create_schema, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, res)

        text = '-----step2: 创建proc; expect: 成功-----'
        log.info(text)
        create_proc = f'''set current_schema to {self.schema_name};
            create proc {self.proc_name}()
            is
            begin
            RAISE INFO 'call procedure: {self.proc_name}';
            end;'''
        res = primary_sh.execut_db_sql(create_proc, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.CREATE_PROCEDURE_SUCCESS_MSG, res)
        detail_sql = f'set current_schema to {self.schema_name};' \
            f'\df {self.proc_name}();'
        res = primary_sh.execut_db_sql(detail_sql, dbname=self.dbname_d)
        log.info(res)
        detail_list = [self.schema_name, self.proc_name, 'void', 
            'normal', 'f', 'p']
        for detail in detail_list:
            self.assertIn(detail, res)
        find_sql = f'set current_schema to {self.schema_name};' \
            f"select provolatile from pg_proc where proname =" \
            f" '{self.proc_name}';"
        res = primary_sh.execut_db_sql(find_sql, dbname=self.dbname_d)
        log.info(res)
        self.assertIn('v', res)

        text = '-----step3: 创建新的schame,修改schame; expect: 成功-----'
        log.info(text)
        create_schema = f'create schema {self.new_schema_name};'
        res = primary_sh.execut_db_sql(create_schema,
                                     dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, res)
        find_sql = f"select nspname from pg_namespace a, pg_proc b " \
            f"where a.oid = b.pronamespace and b.proname = '{self.proc_name}';"
        res = primary_sh.execut_db_sql(find_sql,
                                     dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.schema_name, res)
        alter_sql = f'set current_schema to {self.schema_name};' \
            f'alter proc {self.proc_name}() set ' \
            f'schema {self.new_schema_name};'
        res = primary_sh.execut_db_sql(alter_sql,
                                     dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.ALTER_FUNCTION_SUCCESS_MSG, res)
        res = primary_sh.execut_db_sql(find_sql,
                                     dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.new_schema_name, res)

        text = '-----step4: alter proc 修改schame,proc不带括弧; expect: 合理报错-----'
        log.info(text)
        alter_sql = f'set search_path to {self.new_schema_name};' \
            f'alter proc {self.proc_name} set schema {self.schema_name};'
        res = primary_sh.execut_db_sql(alter_sql, dbname=self.dbname_d)
        log.info(res)
        self.assertIn('ERROR:  syntax error', res)

        text = '-----step5: alter procedure 修改schame; expect: 成功-----'
        log.info(text)
        alter_sql = f'set search_path to {self.new_schema_name};' \
            f'alter proc {self.proc_name}() set schema {self.schema_name};'
        res = primary_sh.execut_db_sql(alter_sql, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.ALTER_FUNCTION_SUCCESS_MSG, res)

    def tearDown(self):
        text = '------------------step6: 清理环境; expect: 成功------------------'
        log.info(text)
        drop_proc = f'''set search_path to {self.schema_name};
            drop proc if exists {self.proc_name};'''
        res = primary_sh.execut_db_sql(drop_proc, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.DROP_PROCEDURE_SUCCESS_MSG, res)
        drop_schema = f'drop schema if exists {self.schema_name};' \
            f'drop schema if exists {self.new_schema_name};'
        res = primary_sh.execut_db_sql(drop_schema, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, res)
